<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">

			<template #content>
				<view class="content" style="height: 100%;width: 100%;">
					<view class="contentBox">
						<view class="container">
							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">

									<uni-forms-item label="检验编号:" label-width="auto">
										<input v-model="baseFormData.BillNO" type="text" placeholder="自动带出检验编号"
											disabled />
									</uni-forms-item>

									<uni-forms-item label="产品名称:" label-width="auto">
										<input v-model="baseFormData.ProductName" type="text" placeholder="自动带出产品名称"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="检验日期:" label-width="auto">
										<input v-model="baseFormData.BillDate" type="text" placeholder="自动带出检验日期"
											disabled />
									</uni-forms-item>

									<uni-forms-item label="检验数量:" label-width="auto">
										<input v-model="baseFormData.CkAmount" type="number" placeholder="请输入检验数量" />
									</uni-forms-item>

									<uni-forms-item label="合格数量:" label-width="auto">
										<input v-model="baseFormData.OkAmount" type="number" placeholder="请输入合格数量"
											@input="addAmount('合格')" />

									</uni-forms-item>
									<uni-forms-item label="不合格数量:" label-width="auto">
										<input v-model="baseFormData.BadAmount" type="number" placeholder="请输入不合格数量"
											@input="addAmount('不合格')" />
									</uni-forms-item>

									<uni-forms-item label="不良率:" label-width="auto">
										<input v-model="baseFormData.BadRate+'%'" type="text" placeholder="自动带出不良率"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="检验结果:" label-width="auto" required>
										<input disabled v-model="baseFormData.ResultName" type="text"
											placeholder="请选择检验结果" />
										<text class="text" @click="changeResultshow"></text>
									</uni-forms-item>
									<uni-forms-item label="审核状态:" label-width="auto">
										<input v-model="baseFormData.ApproveName" type="text" placeholder="请选择审核状态"
											disabled />
										<text class="text" @click="ApproveShow=true"></text>
									</uni-forms-item>
									<uni-forms-item label="审核人:" label-width="auto">
										<input v-model="baseFormData.Approver" type="text" placeholder="自动带出审核人"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="审核时间:" label-width="auto">
										<input v-model="baseFormData.ApproveTime" type="text" placeholder="自动带出审核时间"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="检验员:" label-width="auto">
										<input v-model="baseFormData.QCEmplName" disabled type="text"
											placeholder="请选择检验员" />
										<text class="text" @click="openBaseScreen('人员')"></text>
									</uni-forms-item>
									<uni-forms-item label="派工单编号:" label-width="auto">
										<input v-model="baseFormData.PGBillNo" type="text" placeholder="自动带出派工单编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="工序:" label-width="auto">
										<input v-model="baseFormData.ProcedureName" type="text" placeholder="请选择工序"
											disabled />
									</uni-forms-item>
									<view class="SOPView">
										<u-button size="small" text="查看SOP" @click="addSOP"></u-button>
									</view>

								</view>
							</uni-forms>
						</view>
						<view class="table">
							<table>
								<tr>

									<th>序号</th>
									<th>检验项目</th>
									<th>所属分类</th>

									<th>检验标准</th>
									<th>上极限</th>
									<th>下极限</th>
									<th>实测值</th>
									<th width='25%'>判定结果</th>
								</tr>
								<tr v-for="(item, index) in tableData">

									<td>{{index + 1}}</td>
									<td><input type="text" v-model="item.FQCItemName" disabled></input></td>
									<td><input type="text" v-model="item.InspectionTypeName" disabled></input></td>
									<td><input type="text" v-model="item.JYBZ" disabled></input></td>
									<td><input type="text" v-model="item.PoorWork"></input></td>
									<td><input type="text" v-model="item.PoorNormally"></input></td>
									<td> <input v-if='item.PoorWork&&item.PoorNormally' type="text"
											v-model="item.JYJG"></input>
										<view v-else @click="addJYJG(item,index)">
											{{item.JYJGName}}
										</view>
									</td>
									<td style="text-align: center;">
										<uni-data-checkbox v-model="item.FResult" :localdata="sexs" />
									</td>

								</tr>
							</table>

						</view>
					</view>
					<view class="DetailsbottomBtns">

						<button class="button1" block @click="submit" v-show="buttonShow">
							保存
						</button>
						<button class="button2" block @click="toexamine" v-show="buttonShow">
							审核
						</button>
					</view>
					<u-picker :show="JYJGshow" :columns="columns" @confirm="JYJGconfirm" @cancel='JYJGshow=false'
						@close="JYJGshow=false"></u-picker>

					<u-action-sheet :actions="Resultlist" :show="Resultshow" @select="ResultClick"
						@close="Resultshow=false"></u-action-sheet>


					<u-toast ref="uToast"></u-toast>
					<u-popup :show="popup_logout" mode="center" :closeOnClickOverlay="false" :round="4">
						<view class="popup-content">
							<view class="popup-content_close x-c">
								<uni-icons type="closeempty" color="#999" size="20"
									@click='popup_logout = false'></uni-icons>
							</view>
							<view class="x-c title">
								<text>{{popuptitle}}</text>
							</view>
							<view class="contentView">
								<text>{{popupcontent}}</text>
							</view>
							<view class="btnBox x-ac">
								<u-button v-show="popuptitle=='审核'" type="info" text="取消"
									@click='popup_logout=false'></u-button>
								<u-button type="primary" text="确定" @click="confirmClick"></u-button>
							</view>
						</view>
					</u-popup>

				</view>
			</template>
		</mc-framework>
		<u-loading-page :loading="loadingPage" class="loading-page" :loading-text="loadingName"></u-loading-page>
	</view>
</template>

<script>
	const open = uni.requireNativePlugin("Html5App-openFile");

	export default {
		data() {
			return {
				loadingPage: false,
				loadingName: '',
				popuptitle: '',
				popupcontent: '',
				popup_logout: false,
				baseFormData: {
					InspectionID: 0,
					BillNO: "",
					FType: "",
					ProductID: '',
					ProductName: "",
					BillDate: "",
					CkAmount: null,
					OkAmount: null,
					BadAmount: null,
					BadRate: null,
					Result: null,
					ResultName: '',
					Approve: null,
					ApproveName: '',
					Approver: null,
					ApproveTime: null,
					PGBillNo: "",
					QCEmplID: '',
					QCEmplName: ""
				},
				// 判断结果
				sexs: [{
					text: '合格',
					value: 1
				}, {
					text: '不合格',
					value: -1
				}],
				tableData: [],
				routeIndex: 0,
				JYJGshow: false,
				// 子标的
				columns: [
					['合格', '不合格']
				],
				JYJGshowIndex: 0,
				Resultshow: false,
				Resulttitle: '',
				Resultlist: [],
				ApproveShow: false,
				ApproveList: [{
						name: '未审核',
						id: 1
					},
					{
						name: '已审核',
						id: 0
					}
				],
				buttonShow: true,


			};
		},
		onLoad(onload) {
			var that = this
			that.queryList(onload.InspectionID)
		},
		onShow() {
			this.getValue
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			addAmount(str) {
				var that = this
				var show = true
				if (str == '合格') {

					if (Number(that.baseFormData.CkAmount) < Number(that.baseFormData.OkAmount)) {

						that.popuptitle = '报错'
						that.popupcontent = '合格数量不能大于检验数量'
						that.popup_logout = true
						that.baseFormData.OkAmount = 0
						that.baseFormData.BadAmount = that.baseFormData.CkAmount
						that.$forceUpdate()

					} else if (Number(that.baseFormData.OkAmount) < 0) {
						that.popuptitle = '报错'
						that.popupcontent = '合格数量不能小于0'
						that.popup_logout = true

						that.baseFormData.OkAmount = 0
						that.baseFormData.BadAmount = that.baseFormData.addCkAmount
						setTimeout(() => {
							that.tableData.forEach(item => {
								item.BadNum = 0
							})
							that.baseFormData.BadRate = 0
							that.$forceUpdate()
						}, 500)
						that.$forceUpdate()
					} else {
						that.baseFormData.BadAmount = Number(that.baseFormData.CkAmount) - Number(that.baseFormData
							.OkAmount)
						if (Number(that.baseFormData.CkAmount) > 0) {
							that.baseFormData.BadRate = ((Number(that.baseFormData.BadAmount) / Number(that
								.baseFormData.CkAmount)) * 100).toFixed(2)
						}
						that.$forceUpdate()
					}
				} else {

					if (Number(that.baseFormData.CkAmount) < Number(that.baseFormData.BadAmount)) {
						if (that.baseFormData.CkAmount != '') {


							that.popuptitle = '报错'
							that.popupcontent = '不合格数量不能大于检验数量'
							that.popup_logout = true
							that.baseFormData.BadAmount = 0
							that.baseFormData.OkAmount = that.baseFormData.CkAmount
							setTimeout(() => {
								that.tableData.forEach(item => {
									item.BadNum = 0
								})
								that.baseFormData.BadRate = 0
								that.$forceUpdate()
							}, 500)
							that.$forceUpdate()
						}

					} else if (Number(that.baseFormData.BadAmount) < 0) {
						that.popuptitle = '报错'
						that.popupcontent = '不合格数量不能小于'
						that.popup_logout = true
						that.baseFormData.BadAmount = 0
						that.baseFormData.OkAmount = that.baseFormData.CkAmount
						setTimeout(() => {
							that.tableData.forEach(item => {
								item.BadNum = 0
							})
							that.baseFormData.BadRate = 0
							that.$forceUpdate()
						}, 500)
						that.$forceUpdate()
					} else {
						that.baseFormData.OkAmount = Number(that.baseFormData.CkAmount) - Number(that.baseFormData
							.BadAmount)
						if (Number(that.baseFormData.CkAmount) > 0) {
							that.baseFormData.BadRate = ((Number(that.baseFormData.BadAmount) / Number(that
								.baseFormData.CkAmount)) * 100).toFixed(2)
						}
						that.$forceUpdate()
					}
				}


			},

			getValue(e) {
				var that = this;
				if (e != '返回') {
					console.log(e);
					that.baseFormData.QCEmplID = e.EmplID
					that.baseFormData.QCEmplName = e.name
					that.QCEmplNameShow = false
					console.log(that.baseFormData.QCEmplID);
					that.$forceUpdate()
				}
			},
			openBaseScreen(str) {
				var that = this;
				var obj;
				if (str == '人员') {
					obj = {
						type: str,
						cmd: 'getEmplByJobName',
						param: {
							JobName: '品质'
						}
					}
				}

				uni.navigateTo({
					url: `../common/baseScreen?obj=${JSON.stringify(obj)}`,
					animationType: 'slide-in-right',
					animationDuration: 300
				});
			},




			addSOP() {
				var that = this

				try {
					that.Request.request('getProductSOP', {
						ProductID: that.baseFormData.ProductID
					}, (ret) => {
						that.loadingName = '正在打开中…'
						that.loadingPage = true

						console.log(ret.data);
						if (ret.status == 200) {
							if (ret.data.ds.length > 0) {
								var url = 'http://' + that.Request.serverIP() + ret.data.ds[0].FILEPATH
								console.log(url);
								uni.downloadFile({
									url: url,
									success: function(res) {
										if (res.tempFilePath) {
											var filePath = plus.io.convertLocalFileSystemURL(res
												.tempFilePath);
											open.openFile({
												filename: filePath,
												title: 'SOP查看',
												isShowBackIcon: true,
											}, ret => {
												that.loadingPage = false
											});

										} else {
											that.changeshowToast()
										}
									}
								})
							} else {
								that.changeshowToast()
							}

						}
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},
			changeshowToast() {
				var that = this;
				that.loadingPage = false
				uni.showToast({
					title: '暂无此文档',
					icon: 'error',
					duration: 2000
				})
			},
			ApproveSClick(e) {
				var that = this
				console.log(e);
				that.baseFormData.ApproveName = e.name
				that.baseFormData.Approve = e.id
				that.ApproveShow = false
				that.$forceUpdate()
			},
			ResultClick(e) {
				var that = this
				console.log(e);
				that.baseFormData.Result = e.ID
				that.baseFormData.ResultName = e.name
				that.Resultshow = false
				console.log(that.baseFormData.ResultName);
				that.$forceUpdate()
			},
			changeResultshow() {
				var that = this
				that.Request.request('getJYResult', {
					FType: '首件检验'
				}, (ret) => {
					console.log(ret.data);
					if (ret.status == 200) {
						that.Resultlist = ret.data.ds
						that.Resultlist.map(item => {
							item.name = item.NAME
						})
						that.Resultshow = true
					}
				})

			},
			JYJGconfirm(e) {
				var name = e.value[0]
				var num
				// console.log(e.index[0]);
				that.JYJGshow = false
				if (name == '合格') {
					num = 1
				} else {
					num = -1
				}
				this.tableData[this.JYJGshowIndex].JYJG = num
				this.tableData[this.JYJGshowIndex].JYJGName = name

			},
			addJYJG(item, index) {
				this.JYJGshow = true
				this.JYJGshowIndex = index
			},
			toexamine() {
				var that = this
				var obj = {
					QCEmplName: this.baseFormData.QCEmplName,
					InspectionID: this.baseFormData.InspectionID
				}
				console.log(obj);
				try {

					that.Request.request('ApproveJY', obj, (ret) => {
						console.log(ret.data);
						that.$refs.uToast.show({
							title: '审核',
							type: 'success',
							message: '审核成功'
						})

						setTimeout(() => {
							that.queryList(that.baseFormData.InspectionID)
						}, 1000)
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}

			},
			submit() {
				var that = this
				var show = true
				var message
				if (that.baseFormData.CkAmount == '' || that.baseFormData.CkAmount == null) {
					message = '请输入检验数量'
					show = false
				} else if (that.baseFormData.BadAmount == '' || that.baseFormData.BadAmount == null) {
					message = '请输入不合格数量'
					show = false
				} else if (that.baseFormData.Result == '' || that.baseFormData.Result == null) {
					message = '请输入检验结果'
					show = false
				} else if (that.baseFormData.QCEmplName == '' || that.baseFormData.QCEmplName == null) {
					message = '请输入检验员'
					show = false
				}
				if (show) {
					that.tableData.forEach(item => {
						if (item.PoorWork == '' || item.PoorWork == null) {
							item.PoorWork = '@'
						}
					})
					that.tableData.forEach(item => {
						if (item.PoorNormally == '' || item.PoorNormally == null) {
							item.PoorNormally = '@'
						}
					})
					that.tableData.forEach(item => {
						if (item.JYJG == '' || item.JYJG == null) {
							item.JYJG = '@'
						}
					})
					var obj = {
						// that.tableData.map(item => item.PoorWork).join(','),
						PoorWork: that.tableData.map(item => item.PoorWork).join(','),
						JYJG: that.tableData.map(item => item.JYJG).join(','),
						PoorNormally: that.tableData.map(item => item.PoorNormally).join(','),
						InspectionDeID: that.tableData.map(item => item.InspectionDeID).join(','),
						InspectionID: that.baseFormData.InspectionID,
						FQCItemID: that.tableData.map(item => item.FQCItemID).join(','),
						CkAmount: that.baseFormData.CkAmount,
						BadAmount: that.baseFormData.BadAmount,
						FResult: that.baseFormData.Result,
						QCEmplID: that.baseFormData.QCEmplID
					}
					console.log(obj);
					try {
						that.Request.request('saveJYRecord', obj, (ret) => {
							console.log(ret.data);
							if (ret.status == 200) {
								that.$refs.uToast.show({
									title: '保存',
									type: 'success',
									message: '保存成功'
								})
								setTimeout(() => {
									that.queryList(that.baseFormData.InspectionID)
								}, 1000)
							}
						})
					} catch (e) {
						console.log(e)
						this.$utils.hideLoading();
						this.$utils.msg('请求失败请重试')
					}
				} else {

					that.popuptitle = '报错'
					that.popupcontent = message
					that.popup_logout = true
				}
			},
			changeLevel(data, row) {
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - row.length; // 计算返回页面栈数
				if (backIndex == 1) {
					let prevPage = routes[routes.length - 2]
					prevPage.$vm.getList()
				}
				uni.navigateBack({
					delta: backIndex
				})


			},
			queryList(id) {
				var that = this
				try {
					that.Request.request('getInspectionDetail', {
						InspectionID: id
					}, (ret) => {
						console.log(ret.data);
						if (ret.status == 200) {
							that.baseFormData = ret.data.ds[0]
							that.baseFormData.ApproveTime = that.baseFormData.ApproveTime ? that.baseFormData
								.ApproveTime.replace('T', ' ') : ''
							if (that.baseFormData.ApproveName == '已审核') {
								that.buttonShow = false
							} else {
								that.buttonShow = true
							}
							ret.data.ds1.map(item => {
								if (item.PoorWork == null && item.PoorNormally == null) {
									item.JYJGName = item.JYJG == null ? '' : item.JYJG == 1 ? '合格' : '不合格'
								}
							})
							that.tableData = ret.data.ds1
							that.$forceUpdate()
						}
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";


	.SOPView {
		flex: 1;
		box-sizing: border-box;
		// padding: 1%;
		// background-color: red;

		// box-sizing: ;
		.u-button {
			width: 10%;
			float: right;
			color: #fff;
			background-color: #51d0d0;
			margin-top: 5px;

			box-sizing: border-box;
		}
	}


	.table table {
		table-layout: fixed;

		tr td {
			input {
				width: 100%;
				background-color: #e7e7e7 !important;

			}

			view {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 10px;
			}
		}

	}
</style>